package ir.mirrajabi.searchdialog.core;

import ir.mirrajabi.searchdialog.SimpleSearchFilter;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.TextFilter;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * 封装面板基类
 * Created by MADNESS on 5/14/2017.
 */
public abstract class BaseSearchDialogCompat<T extends Searchable> extends CommonDialog implements Filterable {
    /**
     * 上下文
     */
    protected Context mContext;
    /**
     * 是否自动过滤
     */
    protected boolean mFilterAutomatically = true;
    private TextFilter mFilter;
    private ArrayList<T> mItems;
    private BaseItemProvider mAdapter;
    private FilterResultListener<T> mFilterResultListener;
    private OnPerformFilterListener mOnPerformFilterListener;

    public BaseSearchDialogCompat(Context context) {
        super(context);
        this.mContext = context;
    }

    public BaseSearchDialogCompat(
            Context context,
            ArrayList<T> items,
            TextFilter filter,
            BaseItemProvider adapter,
            FilterResultListener filterResultListener) {
        this(context);
        mItems = items;
        mFilter = filter;
        mAdapter = adapter;
        mFilterResultListener = filterResultListener;
        this.mContext = context;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        Component view = LayoutScatter.getInstance(mContext).parse(getLayoutResId(), null, false);
        getView(view);
        TextField searchBox = (TextField) view.findComponentById(getSearchBoxId());
        ListContainer listContainer = (ListContainer) view.findComponentById(getRecyclerViewId());
        searchBox.addTextObserver(
                new Text.TextObserver() {
                    @Override
                    public void onTextUpdated(String str, int i1, int i2, int i3) {
                        if (isFilterAutomatically()) {
                            getFilter().filter(str);
                        }
                    }
                });
        listContainer.setItemProvider(mAdapter);
    }

    /**
     * 获取view
     * @param view 面板
     */
    protected abstract void getView(Component view);

    /**
     * 布局id
     * @return int
     */
    protected abstract int getLayoutResId();

    /**
     * 搜索框
     * @return int
     */
    protected abstract int getSearchBoxId();

    /**
     * 列表
     * @return int
     */
    protected abstract int getRecyclerViewId();

    /**
     * 过滤器
     * @return TextFilter
     */
    @Override
    public TextFilter getFilter() {
        if (mFilter == null) {
            mFilter = new SimpleSearchFilter<>(mItems, mFilterResultListener, true, 0.33f);
        }
        return mFilter;
    }

    /**
     * 设置过滤器
     * @param filter filter
     * @return BaseSearchDialogCompat
     */
    public BaseSearchDialogCompat setFilter(TextFilter filter) {
        mFilter = filter;
        return this;
    }

    /**
     * 获取列表数据
     * @return ArrayList
     */
    public ArrayList<T> getItems() {
        return mItems;
    }

    /**
     * 设置列表数据
     * @param items items
     * @return BaseSearchDialogCompat
     */
    public BaseSearchDialogCompat setItems(ArrayList<T> items) {
        mItems = items;
        return this;
    }

    /**
     * 获取适配器
     * @return BaseItemProvider
     */
    public BaseItemProvider getAdapter() {
        return mAdapter;
    }

    /**
     * 设置适配器
     * @param adapter adapter
     * @return BaseSearchDialogCompat
     */
    public BaseSearchDialogCompat setAdapter(BaseItemProvider adapter) {
        mAdapter = adapter;
        return this;
    }

    /**
     * 获取过滤器结果监听
     * @return FilterResultListener
     */
    public FilterResultListener<T> getFilterResultListener() {
        return mFilterResultListener;
    }

    /**
     * 设置过滤器结果监听
     * @param filterResultListener filterResultListener
     * @return BaseSearchDialogCompat
     */
    public BaseSearchDialogCompat setFilterResultListener(FilterResultListener<T> filterResultListener) {
        mFilterResultListener = filterResultListener;
        return this;
    }

    /**
     * getOnPerformFilterListener
     * @return OnPerformFilterListener
     */
    public OnPerformFilterListener getOnPerformFilterListener() {
        return mOnPerformFilterListener;
    }

    /**
     * setOnPerformFilterListener
     * @param onPerformFilterListener onPerformFilterListener
     * @return BaseSearchDialogCompat
     */
    public BaseSearchDialogCompat setOnPerformFilterListener(OnPerformFilterListener onPerformFilterListener) {
        mOnPerformFilterListener = onPerformFilterListener;
        return this;
    }

    /**
     * 是否自动
     * @return boolean
     */
    public boolean isFilterAutomatically() {
        return mFilterAutomatically;
    }

    /**
     * 设置是否自动过滤
     * @param filterAutomatically filterAutomatically
     * @return BaseSearchDialogCompat
     */
    public BaseSearchDialogCompat setFilterAutomatically(boolean filterAutomatically) {
        mFilterAutomatically = filterAutomatically;
        return this;
    }
}
